//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace HappySprite.Simulation
{
    public class Box : CollisionShape
    {
        Vector2 halfExtents;

        public Box(float halfX, float halfY)
            : this(new Vector2(halfX, halfY))
        {
        }

        public Box(Vector2 halfExtents)
        {
            this.halfExtents = halfExtents;
        }

        public Box()
            : this(Vector2.One)
        {
        }

        public static Box CreateFromSize(Vector2 size)
        {
            return new Box(size / 2f);
        }

        public static Box CreateFromSize(float width, float height)
        {
            return CreateFromSize(new Vector2(width, height));
        }

        public Vector2 HalfExtents
        {
            get { return halfExtents; }
            set { halfExtents = value; }
        }

        public override ShapeType ShapeType
        {
            get { return ShapeType.Box; }
        }

        public override BoundingRectangle GetBounds(Transform2D t)
        {
            float m11 = Math.Abs(t.M11);
            float m21 = Math.Abs(t.M21);
            float m12 = Math.Abs(t.M12);
            float m22 = Math.Abs(t.M22);

            Vector2 center = t.Position;
            Vector2 extent;

            extent.X = halfExtents.X * m11 + halfExtents.Y * m21;
            extent.Y = halfExtents.X * m12 + halfExtents.Y * m22;

            return new BoundingRectangle(center - extent, center + extent);
        }

        public override Vector2 SupportMapping(Vector2 v)
        {
            return new Vector2(
                (v.X >= 0f) ? halfExtents.X : -halfExtents.X,
                (v.Y >= 0f) ? halfExtents.Y : -halfExtents.Y);
        }
    }
}
